<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">

<xsl:param name="report_title" select="'(Missing Report Title)'"/>

<xsl:key name="regionList" match="region" use="." />

<!-- imports formatting constants and layout functions common to multiple documents -->
<xsl:include href="../include/reportConstants.xsl" /> 
<xsl:include href="../include/reportAttrSets.xsl" /> 
<xsl:include href="../include/reportUtils.xsl" /> 
    
<xsl:decimal-format NaN=""/>

<xsl:template name="get-static-content">
 <fo:static-content flow-name="xsl-region-before">              
        <fo:block text-align="center">
            <xsl:value-of select="$report_title"/>
        </fo:block>
                             	
    <fo:block font-weight="bold" white-space-collapse="false" border-bottom="1pt solid black"></fo:block>                                                                                                       
      		</fo:static-content>
            
            <!-- Footer -->
            <fo:static-content flow-name="xsl-region-after">
            <fo:table table-layout="fixed" font-size="9pt">
                <fo:table-column column-width="10pt * 0.60 * (29 + 0)"/>
                <fo:table-column column-width="10pt * 0.60 * (55 + 0)"/>                                                                    
                    <fo:table-body>
                        <fo:table-row>
                            <fo:table-cell><fo:block>
                                Printed on <xsl:value-of select='/countrySummaryReport/runDate'/>
                            </fo:block></fo:table-cell>
                            <fo:table-cell><fo:block text-align="end">
                                Page <fo:page-number/>
                            </fo:block></fo:table-cell>
                         </fo:table-row>
                    </fo:table-body>
            </fo:table>
            </fo:static-content>
</xsl:template>

<xsl:template match="countrySummaryReport">

   <!-- fo:root is the top element of any printed item (document or book, etc.) -->                           
   <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
       <fo:layout-master-set>
           <fo:simple-page-master master-name="portraitMaster"
                    page-height="11in"
                    page-width="8.5in"
                    margin-top="0.5in"
                    margin-bottom="0.25in"
                    margin-left=".75in"
                    margin-right="0.4in">
               <fo:region-body margin-top=".75in" margin-bottom=".25in"/>
               <fo:region-before extent="1.0in"/>
               <fo:region-after extent=".25in"/>
           </fo:simple-page-master>
       </fo:layout-master-set>
 
       <xsl:choose>
          <xsl:when test="count(./countries/country) =0">
           <fo:page-sequence master-reference="portraitMaster">                   
               <xsl:call-template name="get-static-content"/>
                <fo:flow flow-name="xsl-region-body">				              				                  		                      
                     <fo:block xsl:use-attribute-sets="xsl-region-body-block"> No Records Found</fo:block>
                </fo:flow>
           </fo:page-sequence>		                      
          </xsl:when>
          <xsl:otherwise>		         		          	          	 			          			         
               <xsl:for-each select="./countries/country/region">                  
                   <xsl:if test="generate-id(.) = generate-id(key('regionList', .)[1])">
                       <xsl:apply-templates select="."/>
                   </xsl:if>
               </xsl:for-each>           		    		    
          </xsl:otherwise>
   </xsl:choose>                                               
  </fo:root>
</xsl:template>
    
<xsl:template match="region">
 <fo:page-sequence master-reference="portraitMaster">                   
   <xsl:call-template name="get-static-content"/>
    <fo:flow flow-name="xsl-region-body">
    <fo:block padding-bottom="6pt" font-size="12pt" line-height="13pt">
        <fo:wrapper font-weight="bold">Region: </fo:wrapper> 
        <xsl:value-of select='.'/>          
    </fo:block>			
      	<xsl:variable name="regionVar" select="."/>
        <xsl:apply-templates select="//country[region=$regionVar]"/>
    </fo:flow>
   </fo:page-sequence>
</xsl:template>

<xsl:template match="country">		
	<fo:block number-columns-spanned="2" padding-top="6pt" padding-bottom="6pt" font-size="11pt">
		<fo:wrapper font-weight="bold">Country: </fo:wrapper>
		<xsl:value-of select='./name'/>	(<xsl:value-of select='./isoCode'/>)
	</fo:block>

       <fo:table table-layout="fixed">
    	<fo:table-column column-width="10pt * 0.60 * (15 + 1)"/>
        <fo:table-column column-width="10pt * 0.60 * (15 + 1)"/>
    	<fo:table-column column-width="10pt * 0.60 * (7 + 1)"/>			
    	<fo:table-column column-width="10pt * 0.60 * (7 + 1)"/>
    	<fo:table-column column-width="10pt * 0.60 * (9 + 0)"/>
    	   <fo:table-header xsl:use-attribute-sets="xsl-region-body-block">				               	    		       
      	     <fo:table-row font-weight="bold" white-space-collapse="false">
      		 <fo:table-cell><fo:block>&#10;City</fo:block></fo:table-cell>
                 <fo:table-cell><fo:block>&#10;Language</fo:block></fo:table-cell>
                 <fo:table-cell><fo:block>&#10;Airport</fo:block></fo:table-cell>
                 <fo:table-cell><fo:block text-align="center">Arrival&#10;Flights</fo:block></fo:table-cell>
                 <fo:table-cell><fo:block text-align="center">Departure&#10;Flights</fo:block></fo:table-cell>
             </fo:table-row>  		 
      	   </fo:table-header>
     		 <fo:table-body>
   	             <xsl:apply-templates select="./cities/city"/>
             </fo:table-body>
    </fo:table>                                           	 			
</xsl:template> 

<xsl:template match="city">   
    <fo:table-row font-size="10pt">
        <fo:table-cell><fo:block><xsl:value-of select="./name"/></fo:block></fo:table-cell>        		
        <fo:table-cell><fo:block><xsl:value-of select="./language"/></fo:block></fo:table-cell>
        <fo:table-cell><fo:block><xsl:value-of select="./airport"/></fo:block></fo:table-cell>        		
        <fo:table-cell><fo:block text-align="end"><xsl:value-of select="./arrivalCount"/></fo:block></fo:table-cell>
        <fo:table-cell><fo:block text-align="end"><xsl:value-of select="./departureCount"/></fo:block></fo:table-cell>
    </fo:table-row> 			     			   				 		
</xsl:template>							 		      		
</xsl:stylesheet> 